package cn.yy.logreportmanager.service.impl;

import cn.yy.logreportmanager.dto.common.Response;
import cn.yy.logreportmanager.dto.common.ResponseBean;
import cn.yy.logreportmanager.dto.init.CusMgrEntry;
import cn.yy.logreportmanager.dto.init.DeptMgrDataBody;
import cn.yy.logreportmanager.dto.init.DeptEntry;
import cn.yy.logreportmanager.entity.Department;
import cn.yy.logreportmanager.entity.Employee;
import cn.yy.logreportmanager.mapper.DepartmentMapper;
import cn.yy.logreportmanager.mapper.EmployeeMapper;
import cn.yy.logreportmanager.service.InitService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class InitServiceImpl implements InitService {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public ResponseBean<?> getRelatedDepts(String empID) {
        Employee requester = employeeMapper.selectById(empID);
        if (requester == null){
            return Response.simpleErr("Employee Not Found!", "");
        }

        // ('10000030', '事业部总经理');
        // ('20000010', '营销副总');
        // ('20000020', '营销专员'); --> 以上返回全部部门
        // ('30000010', '销售总监'); --> 返回所属部门
        // ('30000030', '客户经理'); --> 只返回自己
        switch (requester.getEmpPositionId()){
            case "10000030":
            case "20000010":
            case "20000020":
                return getAllDepts();
            case "30000010":
                return getOneDept(requester.getEmpDeptId());
            case "30000030":
                return getOneDeptWithOneMgr(requester.getEmpDeptId(), requester.getEmpId());
            default:
                return Response.simpleErr("员工职位编码错误",
                        "not found: requester: " + requester.getEmpPositionId());
        }
    }

    ResponseBean<?> getAllDepts(){
        DeptMgrDataBody body = new DeptMgrDataBody();
        body.setDepts(new ArrayList<>());

        ResponseBean<DeptMgrDataBody> responseBean = new ResponseBean<>();
        responseBean.setStatus(Response.STATE_OK);
        responseBean.setBody(body);

        List<Department> depts = departmentMapper.selectList(null);
        for (Department department : depts) {
            DeptEntry deptEntry = new DeptEntry();
            deptEntry.setDeptName(department.getDeptName());
            deptEntry.setId(department.getDeptId());
            deptEntry.setCusMgrs(new ArrayList<>());

            List<Employee> employees = employeeMapper.selectList(
                    new LambdaQueryWrapper<Employee>()
                            .eq(Employee::getEmpDeptId, department.getDeptId())
                            .eq(Employee::getEmpPositionId,"30000030") // 仅选择职位为客户经理的
            );
            for (Employee employee : employees) {
                CusMgrEntry cusMgrEntry = new CusMgrEntry();
                cusMgrEntry.setEmpID(employee.getEmpId());
                cusMgrEntry.setEmpName(employee.getEmpName());
                deptEntry.getCusMgrs().add(cusMgrEntry);
            }
            body.getDepts().add(deptEntry);
        }

        return responseBean;
    }

    ResponseBean<?> getOneDept(String deptID){
        DeptMgrDataBody body = new DeptMgrDataBody();
        body.setDepts(new ArrayList<>());

        ResponseBean<DeptMgrDataBody> responseBean = new ResponseBean<>();
        responseBean.setStatus(Response.STATE_OK);
        responseBean.setBody(body);

        List<Department> depts = Collections.singletonList(departmentMapper.selectById(deptID));
        for (Department department : depts) {
            DeptEntry deptEntry = new DeptEntry();
            deptEntry.setDeptName(department.getDeptName());
            deptEntry.setId(department.getDeptId());
            deptEntry.setCusMgrs(new ArrayList<>());

            List<Employee> employees = employeeMapper.selectList(
                    new LambdaQueryWrapper<Employee>()
                            .eq(Employee::getEmpDeptId, department.getDeptId())
                            .eq(Employee::getEmpPositionId, "30000030")
            );
            for (Employee employee : employees) {
                CusMgrEntry cusMgrEntry = new CusMgrEntry();
                cusMgrEntry.setEmpID(employee.getEmpId());
                cusMgrEntry.setEmpName(employee.getEmpName());
                deptEntry.getCusMgrs().add(cusMgrEntry);
            }
            body.getDepts().add(deptEntry);
        }

        return responseBean;
    }

    ResponseBean<?> getOneDeptWithOneMgr(String deptID, String empID){
        DeptMgrDataBody body = new DeptMgrDataBody();
        body.setDepts(new ArrayList<>());

        ResponseBean<DeptMgrDataBody> responseBean = new ResponseBean<>();
        responseBean.setStatus(Response.STATE_OK);
        responseBean.setBody(body);

        List<Department> depts = Collections.singletonList(
                departmentMapper.selectById(deptID)
        );
        for (Department department : depts) {
            DeptEntry deptEntry = new DeptEntry();
            deptEntry.setDeptName(department.getDeptName());
            deptEntry.setId(department.getDeptId());
            deptEntry.setCusMgrs(new ArrayList<>());

            List<Employee> employees = employeeMapper.selectList(
                    new LambdaQueryWrapper<Employee>()
                            .eq(Employee::getEmpDeptId, department.getDeptId())
                            .eq(Employee::getEmpId, empID)
            );
            if (employees.size() == 0) {
                return Response.simpleErr("employee not found!", "");
            }
            for (Employee employee : employees) {
                CusMgrEntry cusMgrEntry = new CusMgrEntry();
                cusMgrEntry.setEmpID(employee.getEmpId());
                cusMgrEntry.setEmpName(employee.getEmpName());
                deptEntry.getCusMgrs().add(cusMgrEntry);
            }
            body.getDepts().add(deptEntry);
        }

        return responseBean;
    }
}
